// pages/classic/classic.js

// 1 import {HTTP} from './../../util/http'
// 1 let http = new HTTP

import {ClassicModel} from '../../models/classic.js'
let classic = new ClassicModel()

import {LikeModel} from '../../models/like.js'
let like = new LikeModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latest: true,
    first: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 2
    // wx.request({
    //   url: 'https://www.easy-mock.com/mock/5d2c4efae46e0e7374ffc3a8/sjjwx/latest',
    //   success: (res) => {
    //     console.log(res)
    //   }
    // })

    // 1
    // http.request({
    //   url: 'latest',
    //   success: (res) => {
    //     console.log(res)
    //   }
    // })
    classic.getLatest((res) => {
      console.log(res)
      this.setData({
        classicData: res.data.result[0]
      })
    })
  },

  onLike: function(event) {
    console.log(event)
    // 获取自定义事件的状态
    let behavior = event.detail.behavior
    like.like(behavior, this.data.classicData.id, this.data.classicData.type)
  },

  // 点击左
  onLeft: function(event) {
    // console.log(event)
    this._updateClassic('left')
  },

  // 点击右边
  onRight: function(event) {
    // console.log(event)
    // let index = this.data.classicData.index
    // console.log(index)
    // classic.getPrevious(index, (res) => {
    //   console.log(res)
    //   this.setData({
    //     classicData: res.data.result[0],
    //     latest: classic.isLatest(res.data.result[0].index),
    //     first: classic.isFirst(res.data.result[0].index)
    //   })
    // })
    this._updateClassic('previous')
  },

  _updateClassic: function(nextOrPrevious) {
    let index = this.data.classicData.index
    console.log(index)
    classic.getClassic(index, nextOrPrevious, (res) => {
      console.log(res)
      this.setData({
        classicData: res.data.result[0],
        latest: classic.isLatest(res.data.result[0].index),
        first: classic.isFirst(res.data.result[0].index)
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})